﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MasMembers.DAL;



namespace MasMembers
{
    public partial class GroupsForm : Form
    {
        public LoginForm loginForm;
        public Int32 currentGroup = -1;


        public GroupsForm()
        {
            InitializeComponent();
        }

        private GroupsNamesEntity fillEntityFromForm()
        {
            GroupsNamesEntity entity = new GroupsNamesEntity();
            entity.G_Name = txtName.Text;
            entity.G_Supervisor = txtSupervisor.Text;
            return entity;
        }

        private void publishFormDataFromEntity(GroupsNamesEntity entity)
        {
            txtName.Text = entity.G_Name;
            txtSupervisor.Text = entity.G_Supervisor;
            currentGroup = entity.G_Id;
        }

        private void clearAllFields()
        {
            txtName.Text = "";
            txtSupervisor.Text = "";
            currentGroup = -1;
        }


        private void RefreshGroupsGrid()
        {
            gridUsers.DataSource = GroupsNames.GetAllGroups();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            GroupsNamesEntity entity = fillEntityFromForm();
            entity.G_Id = currentGroup;
            if (currentGroup == -1 ? GroupsNames.Add(entity) : GroupsNames.Update(entity))
            {
                RefreshGroupsGrid();
                MessageBox.Show("تم الحفظ بنجاح");
                clearAllFields();
            }
            else
            {
                MessageBox.Show("خطأ");
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("هل تريد حذف هذا البيان", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == DialogResult.Yes)
            {
                if (currentGroup != -1 && GroupsNames.Delete(currentGroup))
                {
                    RefreshGroupsGrid();
                    clearAllFields();
                    MessageBox.Show("تم الحذف بنجاح");
                }
                else
                {
                    MessageBox.Show("خطأ");
                }
            }
        }

        private void GroupsForm_Load(object sender, EventArgs e)
        {
            RefreshGroupsGrid();
        }

        private void gridUsers_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != -1 && e.RowIndex != -1)
            {
                List<GroupsNamesEntity> list = (List<GroupsNamesEntity>)gridUsers.DataSource;
                publishFormDataFromEntity(list[e.RowIndex]);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            clearAllFields();
        }
    }
}
